package com.luoshanguo.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luoshanguo.admin.pojo.User;
import com.luoshanguo.admin.mapper.UserMapper;
import com.luoshanguo.admin.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoshanguo.admin.utils.AssertUtil;
import com.luoshanguo.admin.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 罗某人
 * @since 2021-08-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    /**
     * 用户登录
     * @param userName
     * @param password
     * @return
     */
    @Override
    public User login(String userName, String password) {
        //非空判断
        AssertUtil.isTrue(StringUtil.isEmpty(userName),"用户名不能为空");
        AssertUtil.isTrue(StringUtil.isEmpty(password),"用户密码不能为空");
        //引用方法
        User user = this.findUserByName(userName);
        AssertUtil.isTrue(null == user,"该用户记录不存在或已注销");
        AssertUtil.isTrue(!user.getPassword().equals(password),"用户密码错误");
        //返回用户信息
        return user;
    }

    /**
     * 根据用户名查询用户信息
     * @param userName
     * @return
     */
    @Override
    public User findUserByName(String userName) {
        return this.baseMapper.selectOne(new QueryWrapper<User>()
                .eq("is_del",0)
                .eq("user_name",userName));
    }

    /**
     * 用户信息更新
     * @param user
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateUserInfo(User user) {
        AssertUtil.isTrue(StringUtil.isEmpty(user.getUserName()),"用户名不能为空");
        //调用方法判断用户名是否唯一
        User temp = this.findUserByName(user.getUserName());
        AssertUtil.isTrue(null !=temp && !temp.getId().equals(user.getId()),"用户名已存在");
        //更新 判断
        AssertUtil.isTrue(!this.updateById(user),"用户信息更新失败");
    }

    /**
     * 密码更新
     * @return
     */
    @Override
    public void updateUserPassword(String userName, String oldPassword, String newPassword, String confirmPassword) {
        //引用方法
        AssertUtil.isTrue(StringUtil.isEmpty(userName),"用户不存在或未登录");
        AssertUtil.isTrue(StringUtil.isEmpty(oldPassword),"请输入原密码");
        AssertUtil.isTrue(StringUtil.isEmpty(newPassword),"请输入新密码");
        AssertUtil.isTrue(StringUtil.isEmpty(confirmPassword),"请输入确认密码");
        User user = this.findUserByName(userName);
        AssertUtil.isTrue(!user.getPassword().equals(oldPassword),"原密码输入错误");
        AssertUtil.isTrue(user.getPassword().equals(newPassword),"新密码不能与原密码一致");
        AssertUtil.isTrue(!newPassword.equals(confirmPassword),"新密码输入不一致");

        //TODO 密码加密
        user.setPassword(newPassword);
        //更新 判断
        AssertUtil.isTrue(!this.updateById(user),"用户密码更新失败");
    }
}
